package com.li.controller;

import com.li.dao.WorkMapper;
import com.li.pojo.*;
import com.li.service.NewsService;
import com.li.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class WorkController {
    @Autowired
    private WorkService workService;

    @RequestMapping("/selectWork")
    public ReturnMessage selectWork(@RequestParam Map<String, String> params){
        System.out.println("查询参数：");
        System.out.println(params);
        ReturnMessage returnMessage = new ReturnMessage();
        List<WorkDto> list = workService.selectWork(params);
        long count = workService.count(params);
        returnMessage.setData(list);
        returnMessage.setMsg("获取作品数据成功");
        returnMessage.setOther(count);
        return returnMessage;
    }
    @RequestMapping("/addWork")
    public ReturnMessage  addWork(@RequestBody Work work){
        ReturnMessage returnMessage = new ReturnMessage();
        workService.addWork(work);
        returnMessage.setMsg(work.getName()+"添加成功!");
        returnMessage.setData(work);
        return returnMessage;
    }
    @RequestMapping("/updateWork")
    public ReturnMessage updateWork(@RequestBody Work work){
        ReturnMessage returnMessage = new ReturnMessage();
        System.out.println("更新:");
        System.out.println(work);
        workService.updateWork(work);
        returnMessage.setMsg("作品："+work.getName()+"更新成功!");
        returnMessage.setData(work);
        return returnMessage;
    }
    @RequestMapping("/deleteWork")
    public ReturnMessage deleteWork(@RequestBody List<Integer> deleteList){
        ReturnMessage returnMessage = new ReturnMessage();
        int f = workService.deleteWork(deleteList);
        System.out.println("删除");
        System.out.println(deleteList);
        returnMessage.setData(deleteList);
        returnMessage.setMsg("删除成功");
        return returnMessage;
    }
    @RequestMapping("/editWork")
    public ReturnMessage editWork(WorkDto work){
        ReturnMessage returnMessage = new ReturnMessage();
        workService.editWork(work);
        returnMessage.setMsg("更新作品成功");
        return returnMessage;
    }
}
